fix(realtime): cap WebSocket clients + plug writer-goroutine leaks#67
Merged
Conversation
Closes two production-readiness gaps:
1. **Max-clients cap** (codex round 2 finding B): Hub had no admission
limit, so 10k connecting clients would exhaust file descriptors and
per-client send-channel memory before any backpressure kicked in.
New WS_MAX_CLIENTS env var (0 = unlimited, default) and atomic
reservation in HandleWebSocket — over-cap connects return HTTP 503.
2. **Writer-goroutine leak on idle disconnect / Stop**: Pre-existing
bug exposed by the cap test. The writer goroutine blocks on
`for msg := range c.send` and never wakes up when:
- The client disconnects without traffic (no conn.Write to fail on)
- Hub.Stop() is called with idle clients (stopCh return doesn't
close any send channels, so writerWg.Wait() hangs forever)
Fixed by force-closing c.send (CAS-guarded) at two sites:
- HandleWebSocket after reader loop exits — wakes writer on
idle-disconnect so the admission slot releases promptly
- Hub.Run stopCh handler — wakes all writers on Stop so
writerWg.Wait() returns
Tests cover: cap rejection with 503, slot release after disconnect,
unlimited path when cap unset, negative cap coerced to 0, race-detector
clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
PR E of 6. Closes two production-readiness gaps in the WebSocket hub:
Max-clients cap (codex round 2 finding B): no admission limit before — 10k connecting clients would exhaust FDs and per-client send-channel memory. New `WS_MAX_CLIENTS` env var with atomic reservation; over-cap returns HTTP 503.
Writer-goroutine leaks on idle disconnect / Stop (exposed by the cap test): writer blocks on `for msg := range c.send` and never wakes up if the client disconnects without traffic OR if `Stop()` is called with idle clients. Two CAS-guarded close sites added:
Test plan
🤖 Generated with Claude Code